package com.yuke.cloud.service.cmc.controller.frontend;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.dto.CmsCouponUserDto;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.api.wmc.dto.StorageSpecInfoDto;
import com.yuke.cloud.service.api.wmc.dto.WmsGiftDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.cmc.dto.*;
import com.yuke.cloud.service.cmc.entity.CmsCoupon;
import com.yuke.cloud.service.cmc.entity.CmsCouponReceive;
import com.yuke.cloud.service.cmc.entity.CmsSysInfo;
import com.yuke.cloud.service.cmc.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.yuke.cloud.common.util.DateUtil.getDateTimeEnd;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yukemall </br> </p>
 * <p>com.yuke.cloud.service.omc.controller.frontend Description TODO </br> </p>
 * @Author wg
 * @Date   2019/6/5 10:24
 */
@Api(value = "WEB - CmsCouponOrderController", tags = "筛选可用优惠券相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/order")
public class CmsCouponOrderController extends BaseController {
    @Resource
    private CmsCouponReceiveService cmsCouponReceiveService;
    @Resource
    private CmsCouponSpecService cmsCouponSpecService;
    @Resource
    private CmsCouponService cmsCouponService;
    @Resource
    private CmsRedReceiveService cmsRedReceiveService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;
    @Resource
    private CmsSysInfoService cmsSysInfoService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;


    @PostMapping("/coupon")
    @ApiOperation(httpMethod = "POST", value = "获得可用优惠券列表")
    public Wrapper<List<CmsCouponUserDto>> getCouponList(@ApiParam(name = "odsOrderCoupon", value = "该数据记录的POJO") @RequestBody CmsSpecOrderDto cmsSpecOrderDto) {
        return WrapMapper.ok(getCouponUserDtoList(cmsSpecOrderDto));
    }

    @PostMapping("/getLastRealMoney")
    @ApiOperation(httpMethod = "POST", value = "获得扣减优惠后的最终金额")
    public Wrapper<CmsOrderCouponSpecDto> getLastRealMoney(@ApiParam(name = "cmsSpecOrderDto", value = "该数据记录的POJO") @RequestBody CmsSpecOrderDto cmsSpecOrderDto) {
        logger.info("/getLastRealMoney获得扣减优惠券后的最终金额：传入参数cmsSpecOrderDto={}",cmsSpecOrderDto);


        if (cmsSpecOrderDto.getUserId() == null) {
            return WrapMapper.error("用户ID错误！");
        }

        CmsOrderCouponSpecDto cmsOrderCouponSpecDto = new CmsOrderCouponSpecDto();

        //由订单的商品spec_id，查找商品信息
        List<StorageSpecInfoDto> storageSpecInfoDtoList = new ArrayList<>();
        if (cmsSpecOrderDto.getGoodsList() != null && cmsSpecOrderDto.getGoodsList().size() > 0) {
            List<Long> specIds = cmsSpecOrderDto.getGoodsList().stream().map(e -> e.getSpecId()).distinct().collect(Collectors.toList());
            storageSpecInfoDtoList = wmcStorageFeignApi.getWmsSpecInfoBySpecIds(specIds, cmsSpecOrderDto.getShopId());
            if (storageSpecInfoDtoList == null || storageSpecInfoDtoList.size() < 1) {
                return WrapMapper.error("没有找到商品信息！");
            }

        }

        cmsOrderCouponSpecDto.setShopId(cmsSpecOrderDto.getShopId());
        cmsOrderCouponSpecDto.setWeight(cmsSpecOrderDto.getWeight());
        //todo 获得有搭赠的商品做标记，避免返利
        getWmsGiftList(cmsSpecOrderDto, cmsSpecOrderDto.getShopId());
        //todo 获得可用优惠券列表
        //cmsOrderCouponSpecDto.setCouponList(getCouponUserDtoList(cmsSpecOrderDto));
        //todo 获得实际需支付金额
        getRealMoney(cmsSpecOrderDto, storageSpecInfoDtoList);

        cmsOrderCouponSpecDto.setRealMoney(cmsSpecOrderDto.getRealMoney());
        cmsOrderCouponSpecDto.setTotalMoney(cmsSpecOrderDto.getTotalMoney());
        //获得扣减金额
        cmsOrderCouponSpecDto.setDeductionMoney(cmsSpecOrderDto.getDeductionMoney());
        //获得返利金额
        System.out.println("LLLL获得返利金额:"+ cmsSpecOrderDto.getDiscountMoney());
        cmsOrderCouponSpecDto.setDiscountMoney(cmsSpecOrderDto.getDiscountMoney());
        //获取满减金额
        cmsOrderCouponSpecDto.setReductionMoney(cmsSpecOrderDto.getReductionMoney());

        if ((cmsSpecOrderDto.getCouponReceiveId() != null && cmsSpecOrderDto.getCouponReceiveId() > 0 )
                || (cmsSpecOrderDto.getCouponId() != null && cmsSpecOrderDto.getCouponId() > 0) ){
            Wrapper<CmsOrderCouponSpecDto> wrapper = doCouponCount(cmsSpecOrderDto,cmsOrderCouponSpecDto,"getLastRealMoney");

            if (wrapper.getCode() != 200){
                //return wrapper;
            }else{
                cmsOrderCouponSpecDto = wrapper.getResult();

            }

        }

        System.out.println("getLastRealMoney=" + cmsOrderCouponSpecDto);
        logger.info("提交的订单结算后的信息getLastRealMoney=：" + cmsOrderCouponSpecDto);
        return WrapMapper.ok(cmsOrderCouponSpecDto);

    }

    /**
     * 获得结算页面所用信息
     *
     * @param cmsSpecOrderDto
     * @return
     */
    @PostMapping("/deductioncoupon")
    @ApiOperation(httpMethod = "POST", value = "获得结算页面所用信息")
    public Wrapper<CmsOrderCouponSpecDto> getDeductionCouponList(@ApiParam(name = "odsOrderCoupon", value = "该数据记录的POJO") @RequestBody CmsSpecOrderDto cmsSpecOrderDto) {
        System.out.println("cmsSpecOrderDto="+cmsSpecOrderDto);
        CmsOrderCouponSpecDto cmsOrderCouponSpecDto = new CmsOrderCouponSpecDto();
        //由订单的商品spec_id，查找商品信息
        List<StorageSpecInfoDto> storageSpecInfoDtoList = new ArrayList<>();
        if (cmsSpecOrderDto.getGoodsList() != null && cmsSpecOrderDto.getGoodsList().size() > 0) {
            if (cmsSpecOrderDto.getTotalMoney() == null || cmsSpecOrderDto.getTotalMoney() <= 0) {
                return WrapMapper.error("结算金额错误！");
            }

            List<Long> specIds = cmsSpecOrderDto.getGoodsList().stream().map(e -> e.getSpecId()).distinct().collect(Collectors.toList());
            storageSpecInfoDtoList = wmcStorageFeignApi.getWmsSpecInfoBySpecIds(specIds, cmsSpecOrderDto.getShopId());
            if (specIds.size() > storageSpecInfoDtoList.size()) {
                return WrapMapper.error("结算商品错误！");
            }
        }

        //获得搭赠商品列表规则一，已经停用
        //cmsOrderCouponSpecDto.setGiftGoodsList(getGiftSpecList(storageSpecInfoDtoList,cmsSpecOrderDto.getShopId()));
        // todo 获得搭赠商品列表规则二
        /**
         * 1.商品有搭赠：不参加返利；2.商品在赠品库有记录不参加返利；3.赠品不参加返利
         * 4.如果商品为有返利，商品在赠品库有记录，但商品数量未达到，商品库存不足等，返利
         * 5.如果商品为有搭赠，商品在赠品库没有记录，不返利
         * 6。如果商品包含白酒自动使用白酒券
         */
        getWmsGiftList(cmsSpecOrderDto, cmsSpecOrderDto.getShopId());
        //获得结算总金额
        Integer totalMoney = cmsSpecOrderDto.getTotalMoney();
        cmsOrderCouponSpecDto.setTotalMoney(totalMoney);
        logger.info("提交的订单信息：" + cmsSpecOrderDto);

        //todo 获得实际需支付金额(返利后)
        getRealMoney(cmsSpecOrderDto, storageSpecInfoDtoList);

        //todo 获得红包
        List<CmsRedReceiveDto> cmsRedReceiveDtoList = getRedReceiveList(cmsSpecOrderDto);
        if (cmsRedReceiveDtoList != null && cmsRedReceiveDtoList.size() > 0){
            cmsOrderCouponSpecDto.setRedReceiveList(cmsRedReceiveDtoList);
            //提交过来的参数已经选择了红包
            if (cmsSpecOrderDto.getRedReceiveId() != null && cmsSpecOrderDto.getRedReceiveId() > 0){
                cmsOrderCouponSpecDto.setRedReceiveId(cmsSpecOrderDto.getRedReceiveId());
                cmsOrderCouponSpecDto.setRedMoney(cmsSpecOrderDto.getRedMoney());

            }
            //提交过来的参数没有选择红包，默认选择日期最老的那条记录
            else{
                cmsOrderCouponSpecDto.setRedReceiveId(cmsRedReceiveDtoList.get(0).getRedReceiveId());
                cmsOrderCouponSpecDto.setRedMoney(Integer.parseInt(cmsRedReceiveDtoList.get(0).getTypeMoney()));
                cmsSpecOrderDto.setRedReceiveId(cmsRedReceiveDtoList.get(0).getRedReceiveId());
                cmsSpecOrderDto.setRedMoney(Integer.parseInt(cmsRedReceiveDtoList.get(0).getTypeMoney()));

            }
        }




        cmsOrderCouponSpecDto.setTotalMoney(cmsSpecOrderDto.getTotalMoney());
        cmsOrderCouponSpecDto.setOrderGoodsList(cmsSpecOrderDto.getGoodsList());
        cmsOrderCouponSpecDto.setRealMoney(cmsSpecOrderDto.getRealMoney());
        cmsOrderCouponSpecDto.setReductionMoney(cmsSpecOrderDto.getReductionMoney());
        cmsOrderCouponSpecDto.setDiscountMoney(cmsSpecOrderDto.getDiscountMoney());
        cmsOrderCouponSpecDto.setDeductionMoney(cmsSpecOrderDto.getDeductionMoney());

        System.out.println("获得扣减金额1" + cmsOrderCouponSpecDto);
        //todo 获得可用优惠券列表
        List<CmsCouponUserDto> couponList = this.getCouponUserDtoList(cmsSpecOrderDto);
        if(couponList.size() > 0){
            cmsOrderCouponSpecDto.setCouponList(couponList);
            cmsSpecOrderDto.setCouponId(couponList.get(0).getCouponId());
            cmsSpecOrderDto.setCouponReceiveId(couponList.get(0).getCouponReceiveId());
            Wrapper<CmsOrderCouponSpecDto> wrapper = doCouponCount(cmsSpecOrderDto,cmsOrderCouponSpecDto,"getLastRealMoney");

            if (wrapper.getCode() != 200){
                //return wrapper;
            }else{
                cmsOrderCouponSpecDto = wrapper.getResult();

            }
        }

        if (cmsSpecOrderDto.getFreightPayable() != null) {
            cmsSpecOrderDto.setTotalMoney(cmsSpecOrderDto.getRealMoney() + cmsSpecOrderDto.getFreightPayable());
        } else {
            cmsSpecOrderDto.setTotalMoney(cmsSpecOrderDto.getRealMoney());
        }


        //获得商品总重量
        int totalValue = getSpecListWeight(cmsSpecOrderDto.getGoodsList());
        //搭赠商品重量求和
        //totalValue += cmsOrderCouponSpecDto.getGiftGoodsList().stream().mapToInt(CmsSpecDto::getGoodsWeight).sum();

        cmsOrderCouponSpecDto.setWeight(totalValue);
        System.out.println("获得扣减金额2" + cmsOrderCouponSpecDto);
        return WrapMapper.ok(cmsOrderCouponSpecDto);
    }

    //获得可用优惠券列表
    protected List<CmsCouponUserDto> getCouponUserDtoList(CmsSpecOrderDto cmsSpecOrderDto) {

        Long receiveId;
        Integer sendAspect;
        if (cmsSpecOrderDto != null && cmsSpecOrderDto.getOrderType().toString().equals("1")) {
            receiveId = cmsSpecOrderDto.getUserId();
            sendAspect = 2;
        } else {
            receiveId = cmsSpecOrderDto.getShopId();
            sendAspect = 1;
        }
        List<CmsCouponUserDto> list = cmsCouponReceiveService.getCouponListByReceiveId(receiveId, sendAspect);
        List<CmsCouponSpecDto> couponWineList = cmsCouponSpecService.getCouponSpecListByCouponId(29L);
        /**根据订单商品和优惠券筛选出可用的优惠券
         /*1.判断总金额是否满足,时间是否满足
         /*2.判断是否有绑定商品的优惠券
         */
        List<CmsCouponUserDto> cmsCouponUserDtoList = new ArrayList<>();
        if (list != null && list.size() > 0) {

            for (CmsCouponUserDto item : list) {
                //优惠券是否在可用时间范围内
                Date nowdate = new Date();
                if (item.getUseStartDate().getTime() > nowdate.getTime() || item.getUseEndDate().getTime() < nowdate.getTime()) {
                    continue;
                }
                //总金额是否满足
                if (item.getMinAmount() > cmsSpecOrderDto.getRealMoney()) {
                    continue;
                }//if:消费金额大于优惠券门槛金额

                //当优惠券中有包含关联商品的优惠券，执行优惠券绑定商品和订单商品比对
                List<CmsCouponSpecDto> couponspecList = new ArrayList<>();
                if (item.getSendType().toString().equals(GlobalConstant.CouponType.GOODS_COUPON.toString())) {
                    couponspecList = cmsCouponSpecService.getCouponSpecListByCouponId(item.getCouponId());
                } else if (item.getSendType().toString().equals(GlobalConstant.CouponType.WINE_COUPON.toString())) {
                    couponspecList = couponWineList;
                }

                //找优惠券关联商品


                boolean tag = true;

                if (couponspecList != null && couponspecList.size() > 0) {

                    //订单商品遍历
                    for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {

                        //优惠券关联商品遍历
                        for (CmsCouponSpecDto cmsCouponSpecDto : couponspecList) {

                            //当优惠券关联商品等于订单商品
                            if (cmsCouponSpecDto.getSpecId().equals(cmsSpecDto.getSpecId())) {
                                tag = false;

                                break;
                            }

                        }
                    }//for
                    //优惠券不包含订单产品,移除优惠券
                    if (tag) {
                        continue;
                    }
                }//if

                cmsCouponUserDtoList.add(item);

            }//for
        }

        return cmsCouponUserDtoList;
    }

    //获得可用红包列表
    protected List<CmsRedReceiveDto> getRedReceiveList(CmsSpecOrderDto cmsSpecOrderDto) {
        CmsRedReceiveDto cmsRedReceiveDto = new CmsRedReceiveDto();
        cmsRedReceiveDto.setUserId(cmsSpecOrderDto.getUserId());
        cmsRedReceiveDto.setRedStatus((byte) 0);
        List<CmsRedReceiveDto> list = cmsRedReceiveService.selectRedReceiveByCondition(cmsRedReceiveDto);

        /**根据订单商品和优惠券筛选出可用的优惠券
         /*1.判断总金额是否满足,时间是否满足
         /*2.判断是否有绑定商品的优惠券
         */
        List<CmsRedReceiveDto> cmsRedReceiveDtoList = new ArrayList<>();
        if (list != null && list.size() > 0) {

            for (CmsRedReceiveDto item : list) {

                //总金额是否满足
                if (Integer.parseInt(item.getTypeMoney()) > cmsSpecOrderDto.getTotalMoney()) {
                    continue;
                }//if:消费金额大于优惠券门槛金额
                //优惠券是否在可用时间范围内
                //过期检查
                if (item.getRedValidity() != null) {
                    Long expireTime = 0L;
                    //按天数检查
                    System.out.println("优惠券添加时间：" + item.getAddTime().getTime());
                    if (item.getRedValidity() > 0) {
                        expireTime = item.getAddTime().getTime()
                                + 3600 * 24 * 1000 * Long.valueOf(item.getRedValidity().toString())
                                - System.currentTimeMillis();
                    }

                    //当天有效的检查
                    if (item.getRedValidity() < 0) {
                        expireTime = getDateTimeEnd(item.getAddTime()).getTime() - System.currentTimeMillis();
                    }

                    if (expireTime < 0) {
                        continue;
                    }
                    //按红包指定日期的检查
                    if (item.getRedValidity().equals((byte) 0)) {
                        CmsCoupon cmsCoupon = cmsCouponService.findById(item.getCouponId());
                        if (cmsCoupon == null || (cmsCoupon.getUseEndDate().getTime() < (new Date()).getTime())) {
                            continue;
                        }
                    }
                }
                cmsRedReceiveDtoList.add(item);

            }//for
        }

        return cmsRedReceiveDtoList;
    }
//    protected  List<CmsSpecDto> getGiftSpecList(List<StorageSpecInfoDto> prodSpecInfoDtoList, Long shopId){
//        List<CmsSpecDto> cmsSpecDtoList = new ArrayList<>();
//
//        if (prodSpecInfoDtoList!=null && prodSpecInfoDtoList.size()>0){
//            for (StorageSpecInfoDto prodSpecInfoDto : prodSpecInfoDtoList){
//
//                if (prodSpecInfoDto.getHasTying()) {
//                    if (prodSpecInfoDto.getGiftSpecId()!= null && prodSpecInfoDto.getGiftSpecId()!=0){
//                        //ProdSpecInfoDto specInfo = pmcFeignApi.getSpecInfoDtoBySpecId(prodSpecInfoDto.getGiftSpecId());
//                        /* 等优惠券实现分库了后，从wmc拿商品信息*/
//                        List<Long> specIds = new ArrayList<>();
//                        specIds.add(prodSpecInfoDto.getGiftSpecId());
//                        List<StorageSpecInfoDto>  storageSpecInfoDto= wmcStorageFeignApi.getStorageInfoBySpecIds(specIds, shopId);
//                        StorageSpecInfoDto specInfo = storageSpecInfoDto.get(0);
//
//                        CmsSpecDto cmsSpecDto = new CmsSpecDto();
//                        cmsSpecDto.setGoodsName(specInfo.getSpecName());
//                        cmsSpecDto.setGoodsImage(specInfo.getPicture());
//                        cmsSpecDto.setGoodsNum(1);
//                        cmsSpecDto.setSpecId(specInfo.getSpecId());
//                        cmsSpecDto.setGoodsWeight(specInfo.getWeight());
//                        cmsSpecDto.setGoodsPrice(specInfo.getSalePrice());
//                        cmsSpecDtoList.add(cmsSpecDto);
//                    }
//                }
//            }//for
//        }
//
//        return cmsSpecDtoList;
//    }

    protected void getWmsGiftList(CmsSpecOrderDto cmsSpecOrderDto, Long shopId) {
        List<CmsSpecDto> cmsSpecOrderDtos = cmsSpecOrderDto.getGoodsList();
        WmsGiftDto wmsGiftDto = new WmsGiftDto();
        wmsGiftDto.setShopId(shopId);
        wmsGiftDto.setGiftType(cmsSpecOrderDto.getOrderType());
        List<WmsGiftDto> wmsGiftDtoList = wmcStorageFeignApi.getGiftList(wmsGiftDto);
        if (cmsSpecOrderDtos != null && cmsSpecOrderDtos.size() > 0) {
            for (CmsSpecDto orderSpec : cmsSpecOrderDtos) {
                orderSpec.setGoodsAttribute("0");
                for (WmsGiftDto item : wmsGiftDtoList) {
                    if (orderSpec.getSpecId().equals(item.getSpecId())) {
                        int num = 0;
                        //1.是否达到购买数量
                        if (item.getNumber() > 0 && orderSpec.getGoodsNum() < item.getNumber()) {
                            break;
                        }
                        //2.是否达到购买金额
                        if (item.getFee() > 0 && orderSpec.getGoodsNum() * orderSpec.getGoodsPrice() < item.getFee()) {
                            break;
                        }

                        //3.计算出库数量
                        if (item.getNumber() > 0) {
                            num = (int) Math.floor(orderSpec.getGoodsNum() / item.getNumber());
                        }

                        //4.赠品是否还有库存
                        if (item.getStockNumber() - item.getOutNumber() < item.getGiftNum() * num) {
                            break;
                        }
                        //有搭赠
                        orderSpec.setGoodsAttribute("1");

                        break;
                    }

                }//for

            }//for
        }

    }

    protected void getRealMoney(CmsSpecOrderDto cmsSpecOrderDto, List<StorageSpecInfoDto> prodSpecInfoDtoList) {

        Integer totalMoney = 0;
        Integer rebateMoney = 0;


        float discount = 1;
        float businessDiscount = 1;
        float rebate = 0;

        Integer reachMoney = 0;
        Integer returnMoney = 0;

        Integer reachOne = 0;
        Integer reachTwo = 0;
        Integer reachThree = 0;
        Integer returnOne = 0;
        Integer returnTwo = 0;
        Integer returnThree = 0;
        //todo 手机登录，取groupId从userGroup中，
        Long groupId = 4L;
        if (getGroupInfoDto() != null) {
            groupId = getGroupInfoDto().getGroupId();
        } else {
            Wrapper<UserInfoDto> userInfoDtoWrapper = uacUserFeignApi.getUserInfoByUserId(cmsSpecOrderDto.getUserId());
            if (userInfoDtoWrapper.getCode() == 200) {
                if (userInfoDtoWrapper.getResult().getGroupId() != null) {
                    groupId = userInfoDtoWrapper.getResult().getGroupId();
                }

            }
        }
// 如果系统信息需要分库，该段必须启动，替换cmsSysInfoService.findAll()，并且getId也要换成c.getSysInfoType()
        Example example = new Example(CmsSysInfo.class);
        Example.Criteria criteria = example.createCriteria();

        Long shopId = cmsSpecOrderDto.getShopId();
        Long shopGroupId = 220839532667469824L;
        Wrapper wrap = wmcStorageFeignApi.getGroupIdByShopId(shopId);
        if (wrap.getCode() == 200 && wrap.getResult() != null) {
            shopGroupId = Long.valueOf(wrap.getResult().toString());
        }
        criteria.andEqualTo("groupId", shopGroupId);

        List<CmsSysInfo> cmsSysInfoList = cmsSysInfoService.selectByExample(example);


        if (cmsSysInfoList != null && cmsSysInfoList.size() > 0) {
            //获得打折标准
            Optional<CmsSysInfo> cmsSysInfo = cmsSysInfoList.stream().filter((CmsSysInfo c) -> c.getSysInfoType().equals(GlobalConstant.SysInfo.SYS_INFO_DISCOUNT)).findFirst();
            if (cmsSysInfo.isPresent()) {
                CmsSysInfo cmsSysInfoDiscount = cmsSysInfo.get();
                if (cmsSysInfoDiscount != null && cmsSysInfoDiscount.getStatus() > 0) {
                    discount = Float.parseFloat(cmsSysInfoDiscount.getInfoValue());
                }
            }


            //获得B端用户C端打折标准
            Optional<CmsSysInfo> cmsBusinessDiscount = cmsSysInfoList.stream().filter((CmsSysInfo c) -> c.getSysInfoType().equals(GlobalConstant.SysInfo.SYS_INFO_BUSINESS_DISCOUNT)).findFirst();
            if (cmsBusinessDiscount.isPresent()) {
                CmsSysInfo cmsSysInfoBusinessDiscount = cmsBusinessDiscount.get();
                if (cmsSysInfoBusinessDiscount != null && cmsSysInfoBusinessDiscount.getStatus() > 0) {
                    businessDiscount = Float.parseFloat(cmsSysInfoBusinessDiscount.getInfoValue());
                }
            }
            //获得返利标准
            Optional<CmsSysInfo> cmsRebate = cmsSysInfoList.stream().filter((CmsSysInfo c) -> c.getSysInfoType().equals(GlobalConstant.SysInfo.SYS_INFO_REBATE)).findFirst();
            if (cmsRebate.isPresent()) {
                CmsSysInfo cmsSysInfoRebate = cmsRebate.get();
                if (cmsSysInfoRebate != null && cmsSysInfoRebate.getStatus() > 0) {
                    rebate = Float.parseFloat(cmsSysInfoRebate.getInfoValue());
                }
            }
            //获得C端满减标准
            Optional<CmsSysInfo> cmsDeduce = cmsSysInfoList.stream().filter((CmsSysInfo c) -> c.getSysInfoType().equals(GlobalConstant.SysInfo.SYS_INFO_DEDUCE)).findFirst();
            if (cmsDeduce.isPresent()) {
                CmsSysInfo cmsSysInfoDeduce = cmsDeduce.get();
                if (cmsSysInfoDeduce != null && cmsSysInfoDeduce.getStatus() > 0) {
                    JSONObject jsonObject = JSON.parseObject(cmsSysInfoDeduce.getInfoValue());
                    reachMoney = jsonObject.getInteger("reachMoney");
                    returnMoney = jsonObject.getInteger("returnMoney");
                }
            }

            //获得B端满减标准

            Optional<CmsSysInfo> busDeduce = cmsSysInfoList.stream().filter((CmsSysInfo c) -> c.getSysInfoType().equals(21)).findFirst();

            if (busDeduce.isPresent()) {
                CmsSysInfo cmsSysInfoDeduce = busDeduce.get();
                if (cmsSysInfoDeduce != null && cmsSysInfoDeduce.getStatus() > 0) {
                    JSONObject jsonObject = JSON.parseObject(cmsSysInfoDeduce.getInfoValue());
                    reachOne = jsonObject.getInteger("reachOne");
                    reachTwo = jsonObject.getInteger("reachTwo");
                    reachThree = jsonObject.getInteger("reachThree");
                    returnOne = jsonObject.getInteger("returnOne");
                    returnTwo = jsonObject.getInteger("returnTwo");
                    returnThree = jsonObject.getInteger("returnThree");
                }
            }
        }

        if (prodSpecInfoDtoList != null && prodSpecInfoDtoList.size() > 0) {
            if (cmsSpecOrderDto.getGoodsList() != null && cmsSpecOrderDto.getGoodsList().size() > 0) {
                // todo 20191026临时修改为2，下个版本改为0
                if (cmsSpecOrderDto.getOrderType() == null) {
                    cmsSpecOrderDto.setOrderType((byte) 2);
                }
                float price = 0;
                for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                    cmsSpecDto.setPayPrice(cmsSpecDto.getGoodsPrice());
                    System.out.println("商品价格：" + cmsSpecDto.getGoodsPrice() +" * " + cmsSpecDto.getGoodsNum() + " = " + cmsSpecDto.getGoodsPrice() * cmsSpecDto.getGoodsNum());
                    for (StorageSpecInfoDto prodSpecInfoDto : prodSpecInfoDtoList) {
                        if (cmsSpecDto.getSpecId().equals(prodSpecInfoDto.getSpecId())) {
                            cmsSpecDto.setStorageId(prodSpecInfoDto.getStorageId());
                            if (cmsSpecDto.getGoodsNum() == null) {
                                cmsSpecDto.setGoodsNum(1);
                            }
                            if (cmsSpecDto.getGoodsPrice() == null) {
                                cmsSpecDto.setGoodsPrice(cmsSpecOrderDto.getOrderType().equals((byte) 2) ? prodSpecInfoDto.getStockPrice() : prodSpecInfoDto.getMarketPrice());
                            }
                            totalMoney += cmsSpecDto.getGoodsPrice() * cmsSpecDto.getGoodsNum();
                            System.out.println("商品总价： " + totalMoney);
                            //1.该商品是否打折
                            if (prodSpecInfoDto.getHasDiscount() > 0) {
                                //订单为消费者下单，可打折
                                if (cmsSpecOrderDto.getOrderType().toString().equals("1")) {
                                    //如果商家用户、云顶商人在C端下单打7折，否则按C端的正常折扣
                                    if (groupId > 4) {
                                        cmsSpecDto.setPayPrice(Math.round(cmsSpecDto.getGoodsPrice() * businessDiscount));
                                    } else {
                                        cmsSpecDto.setPayPrice(Math.round(cmsSpecDto.getGoodsPrice() * discount));
                                    }
                                }
                            }

                            //2.是否搭赠,搭赠不参加返利
                            boolean tag =false;
                            if (!prodSpecInfoDto.getHasTying() && !cmsSpecDto.getGoodsAttribute().equals("1")) {
                                //3.是否返利
                                if (prodSpecInfoDto.getHasRebate() > 0) {

                                    //订单为店家可返利
                                    if (cmsSpecOrderDto.getOrderType().toString().equals("2")) {
                                        cmsSpecDto.setPayPrice(Math.round(cmsSpecDto.getGoodsPrice() * (1 - rebate)));
                                        tag=true;
                                    }
                                }
                            }
                            if (tag){
                                price += cmsSpecDto.getGoodsPrice() * cmsSpecDto.getGoodsNum() * (1 - rebate);

                            }else{
                                price += cmsSpecDto.getPayPrice() * cmsSpecDto.getGoodsNum();
                            }

                            break;
                        }//if id=id
                    }//for
                }//for
                rebateMoney = Math.round(price);
                cmsSpecOrderDto.setDiscountMoney(totalMoney -rebateMoney);
                //4.是否满减 满减活动是否开启 满减是针对C端
                if (reachMoney > 0) {
                    if (cmsSpecOrderDto.getOrderType().toString().equals("1")) {
                        if (rebateMoney >= reachMoney) {

                            for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                                float rate = (float)returnMoney / rebateMoney;
                                cmsSpecDto.setPayPrice(Math.round(cmsSpecDto.getPayPrice() *(1- rate) ));
                            }

                            rebateMoney = rebateMoney - returnMoney;
                            cmsSpecOrderDto.setReductionMoney(returnMoney);
                        }
                    }
                }

                //5.是否满减 满减活动是否开启 满减是针对B端
                if (reachOne != null && reachOne > 0) {
                    if (cmsSpecOrderDto.getOrderType().toString().equals("2")) {
                        if (rebateMoney >= reachThree) {
                            returnMoney = returnThree;
                        } else if (rebateMoney >= reachTwo) {
                            returnMoney = returnTwo;
                        } else if (rebateMoney >= reachOne) {
                            returnMoney = returnOne;
                        }

                        for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                            float rate = (float)returnMoney / rebateMoney;
                            cmsSpecDto.setPayPrice(Math.round(cmsSpecDto.getPayPrice() *(1- rate) ));
                        }

                        rebateMoney = rebateMoney - returnMoney;
                        cmsSpecOrderDto.setReductionMoney(returnMoney);
                    }
                }
            }//if
        }//if

        cmsSpecOrderDto.setTotalMoney(totalMoney);
        cmsSpecOrderDto.setRealMoney(rebateMoney);
        cmsSpecOrderDto.setDeductionMoney(totalMoney - rebateMoney);

    }

    protected Integer getSpecListWeight(List<CmsSpecDto> cmsSpecDtoList) {
        Integer weight = 0;
        if (cmsSpecDtoList != null && cmsSpecDtoList.size() > 0) {
            for (CmsSpecDto item : cmsSpecDtoList) {
                if (item.getGoodsWeight() != null) {
                    weight += item.getGoodsNum() * item.getGoodsWeight();
                }
            }
        }
        return weight;
    }

    /**
     * 货到付款转线上支付，重新结算
     * @param cmsSpecOrderDto
     * @return
     */
    @PostMapping("/getRePayMoney")
    @ApiOperation(httpMethod = "POST", value = "获得扣减优惠后的最终金额")
    public Wrapper<CmsOrderCouponSpecDto> getRePayMoney(@ApiParam(name = "cmsSpecOrderDto", value = "该数据记录的POJO") @RequestBody CmsSpecOrderDto cmsSpecOrderDto) {
        System.out.println("再次结算：提交给后台的参数" + cmsSpecOrderDto);
        if(cmsSpecOrderDto == null || cmsSpecOrderDto.getPayType() == null){
            return WrapMapper.error("需要提交原订单的支付方式-payType！");
        }
        //由订单的商品spec_id，查找商品信息
        List<StorageSpecInfoDto> storageSpecInfoDtoList = new ArrayList<>();
        if (cmsSpecOrderDto.getGoodsList() != null && cmsSpecOrderDto.getGoodsList().size() > 0) {
            List<Long> specIds = cmsSpecOrderDto.getGoodsList().stream().map(e -> e.getSpecId()).distinct().collect(Collectors.toList());
            storageSpecInfoDtoList = wmcStorageFeignApi.getWmsSpecInfoBySpecIds(specIds, cmsSpecOrderDto.getShopId());
            if (storageSpecInfoDtoList == null || storageSpecInfoDtoList.size() < 1) {
                return WrapMapper.error("没有找到商品信息！");
            }

        }

        //todo 获得有搭赠的商品做标记，避免返利
        getWmsGiftList(cmsSpecOrderDto, cmsSpecOrderDto.getShopId());
        //todo 获得实际需支付金额
        getRealMoney(cmsSpecOrderDto, storageSpecInfoDtoList);
        CmsOrderCouponSpecDto cmsOrderCouponSpecDto = new CmsOrderCouponSpecDto();
        cmsOrderCouponSpecDto.setShopId(cmsSpecOrderDto.getShopId());
        cmsOrderCouponSpecDto.setWeight(cmsSpecOrderDto.getWeight());
        cmsOrderCouponSpecDto.setRealMoney(cmsSpecOrderDto.getRealMoney());
        cmsOrderCouponSpecDto.setTotalMoney(cmsSpecOrderDto.getTotalMoney());
        //获得扣减金额
        cmsOrderCouponSpecDto.setDeductionMoney(cmsSpecOrderDto.getDeductionMoney());
        //获得返利金额
        cmsOrderCouponSpecDto.setDiscountMoney(cmsSpecOrderDto.getDiscountMoney());
        //获取满减金额
        cmsOrderCouponSpecDto.setReductionMoney(cmsSpecOrderDto.getReductionMoney());
        //获得可用优惠券列表
        cmsOrderCouponSpecDto.setCouponList(this.getCouponUserDtoList(cmsSpecOrderDto));

        cmsOrderCouponSpecDto.setOrderGoodsList(cmsSpecOrderDto.getGoodsList());

        if (cmsSpecOrderDto.getCouponReceiveId() != null && cmsSpecOrderDto.getCouponReceiveId() >0) {
            cmsOrderCouponSpecDto.setCouponReceiveId(cmsSpecOrderDto.getCouponReceiveId());

            Wrapper<CmsOrderCouponSpecDto> wrapper = this.doCouponCount(cmsSpecOrderDto,cmsOrderCouponSpecDto,"getRePayMoney");

            if (wrapper.getCode() != 200){
                cmsOrderCouponSpecDto.setCouponReceiveId(0L);
                cmsOrderCouponSpecDto.setCouponMoney(0);
                logger.info(wrapper.getMessage());
            }else {
                cmsOrderCouponSpecDto = wrapper.getResult();

                CmsCouponUserDto cmsCouponUserDto = new CmsCouponUserDto();
                cmsCouponUserDto.setCouponReceiveId(cmsSpecOrderDto.getCouponReceiveId());
                List<CmsCouponUserDto> list = cmsCouponReceiveService.getCouponUserListByCondition(cmsCouponUserDto);

                if (list.get(0).getCouponStatus().equals((byte)1)){
                    //如果这个优惠券是已使用状态，必须把这个优惠券绑到这个订单上，排除其他优惠券
                    cmsOrderCouponSpecDto.setCouponList(list);
                }else{
                    //如果优惠券列表中没有当前的这个优惠券，把这个优惠券添加到优惠券列表中
                    boolean tag = true;
                    for (CmsCouponUserDto item : cmsOrderCouponSpecDto.getCouponList()){
                        if (item.getCouponReceiveId().equals(list.get(0).getCouponReceiveId())){
                            tag=false;
                            break;
                        }
                    }
                    if (tag){
                        cmsOrderCouponSpecDto.getCouponList().add(list.get(0));
                    }
                }


                System.out.println("重新提交返回的优惠券信息："+ cmsOrderCouponSpecDto.getCouponList());

            }
        }else{
            cmsOrderCouponSpecDto.setCouponReceiveId(0L);
            cmsOrderCouponSpecDto.setCouponMoney(0);
        }

        //获得可用红包
        List<CmsRedReceiveDto> cmsRedReceiveDtoList = getRedReceiveList(cmsSpecOrderDto);
        //提交过来的参数已经选择了红包
        if (cmsSpecOrderDto.getRedReceiveId() != null && cmsSpecOrderDto.getRedReceiveId() > 0){
            cmsOrderCouponSpecDto.setRedReceiveId(cmsSpecOrderDto.getRedReceiveId());
            cmsOrderCouponSpecDto.setRedMoney(cmsSpecOrderDto.getRedMoney());

            //把已经选择的红包加入到可用红包列表中
            CmsRedReceiveDto cmsRedReceiveDto = new CmsRedReceiveDto();
            cmsRedReceiveDto.setRedReceiveId(cmsSpecOrderDto.getRedReceiveId());
            List<CmsRedReceiveDto> list = cmsRedReceiveService.selectRedReceiveByCondition(cmsRedReceiveDto);

            cmsRedReceiveDtoList.add(list.get(0));
        }else{
            cmsOrderCouponSpecDto.setRedReceiveId(0L);
            cmsOrderCouponSpecDto.setRedMoney(0);
        }

        if (cmsRedReceiveDtoList != null && cmsRedReceiveDtoList.size() > 0){
            cmsOrderCouponSpecDto.setRedReceiveList(cmsRedReceiveDtoList);
        }
        cmsOrderCouponSpecDto.setOrderGoodsList(cmsSpecOrderDto.getGoodsList());

        System.out.println("再次提交订单的结算返回参数=" + cmsOrderCouponSpecDto);
        return WrapMapper.ok(cmsOrderCouponSpecDto);

    }

    private  Wrapper<CmsOrderCouponSpecDto> doCouponCount(CmsSpecOrderDto cmsSpecOrderDto,CmsOrderCouponSpecDto cmsOrderCouponSpecDto ,String tag){
        //先判断该优惠券能否使用
        CmsCouponReceive ccr = new CmsCouponReceive();

        if (cmsSpecOrderDto.getCouponReceiveId() != null ){
            ccr.setCouponReceiveId(cmsSpecOrderDto.getCouponReceiveId());
        }else{
            ccr.setCouponId(cmsSpecOrderDto.getCouponId());
        }
        //ccr.setCouponStatus((byte)0);
        ccr.setReceiveId(cmsSpecOrderDto.getShopId());
        ccr.setReceiveType((byte) 1);
        List<CmsCouponReceive> cmsCouponReceiveList = cmsCouponReceiveService.select(ccr);
        if (cmsCouponReceiveList == null || cmsCouponReceiveList.size() < 1) {
            return WrapMapper.error("没有找到领取的可用优惠券！");
        }

        if ("1".equals(cmsCouponReceiveList.get(0).getCouponStatus().toString())){
            System.out.println("重新提交的订单ID="+ cmsSpecOrderDto.getOrderId());
            if (cmsSpecOrderDto.getOrderId() == null || cmsSpecOrderDto.getOrderId() < 1){
                return WrapMapper.error("该优惠券已经被其他订单使用！");
            }
            if (cmsSpecOrderDto.getOrderId() != null && !cmsSpecOrderDto.getOrderId().equals(cmsCouponReceiveList.get(0).getOrderId())){
                return WrapMapper.error("该优惠券已经被其他订单使用！");
            }

        }

        //检查可用优惠券
        CmsCoupon cmsCoupon = new CmsCoupon();
        cmsCoupon.setCouponId(cmsCouponReceiveList.get(0).getCouponId());
        List<CmsCoupon> cmsCouponList = cmsCouponService.select(cmsCoupon);

        if (cmsCouponList == null || cmsCouponList.size() < 1) {
            return WrapMapper.error("没有找到可用优惠券！");
        } else {
            cmsCoupon = cmsCouponList.get(0);
            //计算是否在使用期
            Date nowdate = new Date();
            if (cmsCoupon.getUseStartDate().getTime() > nowdate.getTime()) {
                return WrapMapper.error("优惠券还未到使用期！");
            }
            if (cmsCoupon.getUseEndDate().getTime() < nowdate.getTime()) {
                return WrapMapper.error("优惠券已过期！");
            }
            //计算是否达到可用金额

            if (cmsOrderCouponSpecDto.getRealMoney() < cmsCoupon.getMinAmount()) {
                return WrapMapper.error("订单没有达到优惠券最小使用金额！");
            }
            Integer couponDeduceMoney = 0;
            /**
             * 如果优惠券和商品绑定,订单商品是否可用优惠券
             */
            if (cmsCoupon.getSendType().toString().equals(GlobalConstant.CouponType.GOODS_COUPON.toString())) {
                List<CmsCouponSpecDto> couponspecList = cmsCouponSpecService.getCouponSpecListByCouponId(cmsSpecOrderDto.getCouponId());
                if (couponspecList != null && couponspecList.size() > 0) {
                    Integer orderMoney = 0;
                    boolean btag = false;
                    int num = 0;
                    for (CmsCouponSpecDto item : couponspecList) {
                        for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                            if (item.getSpecId().equals(cmsSpecDto.getSpecId())) {
                                orderMoney += cmsSpecDto.getPayPrice() * cmsSpecDto.getGoodsNum();
                                num += cmsSpecDto.getGoodsNum();
                                btag = true;
                                break;
                            }
                        }
                    }

                    if (!btag) {
                        return WrapMapper.error("订单没有优惠券指定商品！");
                    }
                    if (num < cmsCoupon.getMinGoodsAmount()) {
                        return WrapMapper.error("订单商品金额没有达到优惠券指定商品最低数量！");
                    }
                    if (orderMoney < cmsCoupon.getMinAmount()) {
                        return WrapMapper.error("订单商品金额没有达到优惠券指定商品最低金额！");
                    }

                    cmsOrderCouponSpecDto.setRealMoney(cmsOrderCouponSpecDto.getRealMoney() - cmsCoupon.getTypeMoney());
                    cmsOrderCouponSpecDto.setDeductionMoney(cmsSpecOrderDto.getDeductionMoney() + cmsCoupon.getTypeMoney());
                    couponDeduceMoney = cmsCoupon.getTypeMoney();

                    //处理优惠金额均摊到每个商品。

                    for (CmsCouponSpecDto item : couponspecList) {
                        for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                            if (item.getSpecId().equals(cmsSpecDto.getSpecId())) {
                                cmsSpecDto.setPayPrice(Math.round(cmsSpecDto.getPayPrice() - cmsSpecDto.getPayPrice() * cmsCoupon.getTypeMoney() / orderMoney));
                                break;
                            }
                        }
                    }

                }

            } else if (cmsCoupon.getSendType().toString().equals(GlobalConstant.CouponType.WINE_COUPON.toString())) {
                //白酒优惠券
                List<CmsCouponSpecDto> couponspecList = cmsCouponSpecService.getCouponSpecListByCouponId(cmsSpecOrderDto.getCouponId());
                if (couponspecList != null && couponspecList.size() > 0) {
                    Integer orderMoney = 0;
                    boolean btag = false;
                    int num = 0;
                    for (CmsCouponSpecDto item : couponspecList) {
                        for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                            if (item.getSpecId().equals(cmsSpecDto.getSpecId())) {
                                orderMoney = orderMoney + cmsSpecDto.getGoodsPrice() * cmsSpecDto.getGoodsNum();
                                num += cmsSpecDto.getGoodsNum();
                                btag = true;
                                break;
                            }
                        }
                    }

                    if (!btag) {
                        return WrapMapper.error("订单没有优惠券指定商品！");
                    }
                    if (num < cmsCoupon.getMinGoodsAmount()) {
                        return WrapMapper.error("订单商品金额没有达到优惠券指定商品最低数量！");
                    }
                    if (orderMoney < cmsCoupon.getMinAmount()) {
                        return WrapMapper.error("订单商品金额没有达到优惠券指定商品最低金额！");
                    }

                    System.out.println("使用白酒优惠券，每件白酒减2元");
                    cmsOrderCouponSpecDto.setRealMoney(cmsOrderCouponSpecDto.getRealMoney() - cmsCoupon.getTypeMoney() * num);
                    cmsOrderCouponSpecDto.setDeductionMoney(cmsSpecOrderDto.getDeductionMoney() + cmsCoupon.getTypeMoney() * num);
                    couponDeduceMoney = cmsCoupon.getTypeMoney() * num;

                    for (CmsCouponSpecDto item : couponspecList) {
                        for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                            if (item.getSpecId().equals(cmsSpecDto.getSpecId())) {
                                cmsSpecDto.setPayPrice(cmsSpecDto.getPayPrice() - cmsCoupon.getTypeMoney());
                                break;
                            }
                        }
                    }

                }

            } else {
                for (CmsSpecDto cmsSpecDto : cmsSpecOrderDto.getGoodsList()) {
                    float rate = (float)cmsCoupon.getTypeMoney() / cmsOrderCouponSpecDto.getRealMoney();
                    cmsSpecDto.setPayPrice(Math.round(cmsSpecDto.getPayPrice() *(1- rate)));
                }

                cmsOrderCouponSpecDto.setRealMoney(cmsOrderCouponSpecDto.getRealMoney() - cmsCoupon.getTypeMoney());
                cmsOrderCouponSpecDto.setDeductionMoney(cmsSpecOrderDto.getDeductionMoney() + cmsCoupon.getTypeMoney());
                couponDeduceMoney = cmsCoupon.getTypeMoney();
            }

            System.out.println("优惠券金额："+couponDeduceMoney);

            cmsOrderCouponSpecDto.setCouponMoney(couponDeduceMoney);

            //如果有包邮优惠券
            if (cmsCoupon.getSendType().toString().equals(GlobalConstant.CouponType.EXPRESS_COUPON.toString()) && cmsSpecOrderDto.getFreightPayable() != null) {
                cmsOrderCouponSpecDto.setWeight(0);
                cmsOrderCouponSpecDto.setDeductionMoney(cmsSpecOrderDto.getDeductionMoney() + cmsSpecOrderDto.getFreightPayable());
                cmsOrderCouponSpecDto.setCouponMoney(cmsSpecOrderDto.getFreightPayable());
            } else {
                if (cmsSpecOrderDto.getFreightPayable() != null) {
                    cmsOrderCouponSpecDto.setRealMoney(cmsOrderCouponSpecDto.getRealMoney() + cmsSpecOrderDto.getFreightPayable());

                } else {
                    cmsOrderCouponSpecDto.setRealMoney(cmsOrderCouponSpecDto.getRealMoney());
                }
            }

            cmsOrderCouponSpecDto.setOrderGoodsList(cmsSpecOrderDto.getGoodsList());


        }
        Long crId = cmsCouponReceiveList.get(0).getCouponReceiveId();
        cmsOrderCouponSpecDto.setCouponReceiveId(crId);
        System.out.println("如果此单使用了优惠券，保存到redis标记使用状态" + crId);
        if (crId != null) {
            if (crId != 0) {
                // cmsCouponReceiveService.modifyCouponStatus(crId,0L);
                String id = "CouponReceiveIdAndUserId:" + cmsSpecOrderDto.getUserId();
                Map<String, String> map = new HashMap();
                map.put("CouponReceiveId", crId.toString());
                map.put("userId", cmsSpecOrderDto.getUserId().toString());
                map.put("shopId", cmsSpecOrderDto.getShopId().toString());
                map.put("totalMoney", cmsSpecOrderDto.getTotalMoney().toString());
                redisTemplate.opsForValue().set(id, map, 5 * 60 * 60 + 60, TimeUnit.SECONDS);
            }

        }

        return WrapMapper.ok(cmsOrderCouponSpecDto);
    }


}
